<template>
  <div>
    <el-form labelWidth="120px" :disabled="status==1">
      <el-form-item label="申请部门" class="is-required">
        <selectorg @getselectorgid="getselectorgid" :editorgid="editorgid"></selectorg>
        <div class="el-form-item__error" v-if="!$v.carData.branchid.required">请选择申请部门</div>
      </el-form-item>
      <el-form-item label="申请时间" class="is-required">
        <el-date-picker
          v-model="carData.examintime"
          type="date"
          style="width:93%;"
          placeholder="选择申请时间"
          valueFormat="yyyy-MM-dd"
          clearable
        ></el-date-picker>
        <div class="el-form-item__error" v-if="!$v.carData.examintime.required">请选择申请时间</div>
      </el-form-item>
      <el-form-item label="车牌号" class="is-required">
        <selectcarnumber @getpersonid2="getpersonid2" :itemtype="2" :careditid="careditid"></selectcarnumber>
        <div class="el-form-item__error" v-if="!$v.carData.carid.required">请选择车牌号</div>
      </el-form-item>
      <el-form-item label="公里数(km)" class="is-required">
        <el-input v-model="carData.mileage" placeholder="请输入公里数" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.carData.mileage.required">请输入公里数</div>
      </el-form-item>

      <el-form-item label="申请项目" class="is-required">
        <el-select v-model="carData.codeid" placeholder="请选择" style="width:100%">
          <el-option
            v-for="item in codeidArr"
            :key="item.value"
            :label="item.hash.code_name"
            :value="item.hash.code_id"
          ></el-option>
        </el-select>
        <div class="el-form-item__error" v-if="!$v.carData.codeid.required">请选择申请项目</div>
      </el-form-item>
      <el-form-item label="维修地点" class="is-required">
        <el-input v-model="carData.address" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.carData.address.required">请输入维修地点</div>
      </el-form-item>
      <el-form-item label="申请金额(元)" class="is-required">
        <el-input v-model="carData.amount" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.carData.amount.required">请输入申请金额</div>
        <div class="el-form-item__error" v-if="!$v.carData.amount.money">金额格式有误</div>
      </el-form-item>
      <el-form-item label="大写金额">{{this.convertCurrency(carData.amount)}}</el-form-item>
    </el-form>
    <el-form labelWidth="120px" :disabled="status==1">
      <el-form-item label="维修明细" class="is-required" style="width:100%">
        <el-input v-model="carData.detail" clearable></el-input>
        <div class="el-form-item__error" v-if="!$v.carData.detail.required">请输入维修明细</div>
      </el-form-item>
      <el-form-item label="维修备注" style="width:100%">
        <el-input v-model="carData.beizhu" clearable></el-input>
      </el-form-item>
      <el-form-item label="维修附件" style="width:100%">
        <upload2 :filelimit="5" :filelink.sync="filearr1" :ispic="false" :isedit="false"></upload2>
      </el-form-item>
      <el-form-item label="选择审批流" v-if="status!=1" class="is-required" style="width:100%">
        <choosespl @getsplid="getsplid" :atypeid="'0104'"></choosespl>
        <div class="el-form-item__error" v-if="!$v.aid.required">请选择审批流</div>
      </el-form-item>
    </el-form>

    <div style="text-align:right" v-if="status==0">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitcar">确 定</el-button>
    </div>
    <div style="text-align:right" v-if="status==2">
      <el-button size="mini" @click="closedia">取 消</el-button>
      <el-button size="mini" type="primary" @click="submitcar2">重新提交</el-button>
    </div>
  </div>
</template>

<script>
import selectorg from '~/components/common/selectorg.vue';
import selectcarnumber from '~/components/common/selectcarnumber.vue';
import choosespl from '~/components/apply/choosespl.vue';
import upload2 from '~/components/common/upload2.vue';
import { required } from 'vuelidate/lib/validators';
import { regex } from 'vuelidate/lib/validators/common.js';

const money = regex('money', /^\d{1,8}(\.\d{1,2})?$/);

export default {
  validations: {
    aid: {
      required,
    },
    carData: {
      branchid: {
        required,
      },
      examintime: {
        required,
      },
      carid: {
        required,
      },
      mileage: {
        required,
      },
      amount: {
        required,
        money,
      },
      codeid: {
        required,
      },
      address: {
        required,
      },
      detail: {
        required,
      },
    },
  },
  data() {
    return {
      flag: false,
      dialogVisible: true,
      ts: this.newStore(),
      filearr1: [],
      codeidArr: [],
      //   photoarr: [],
      carData: {},
      aid: '',
      editorgid: '',
      careditid: '',
    };
  },
  components: {
    selectorg,
    choosespl,
    upload2,
    selectcarnumber,
  },
  props: {
    status: Number,
    checkdetail: Object,
    fun: Function,
  },
  created() {
    this.getcodeData('0301', '03', 'codeidArr');
    if (this.status != 0) {
      for (let i = 0; i < this.checkdetail.operation2.length; i += 1) {
        const a = {};
        a.url = this.checkdetail.operation2[i].hash.fileurl;
        a.name = this.checkdetail.operation2[i].hash.filename;
        this.filearr1.push(a);
      }

      this.carData = this.checkdetail.operation1[0].hash;

      this.editorgid = this.carData.branchid;
      this.careditid = this.carData.carno;
      console.log(this.careditid, '0909');
    }
  },
  methods: {
    checksplnum(money2, splid) {
      const req = {
        handle: '9000B8B8D347526960C5BAB1FB6894EF3277431D02D636A5015C1425899A1F59A41CF7A197311E4A',
        flowid: splid,
        number: money2,
      };
      const p = this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.data.operation1[0].hash.cou == '0') {
          this.flag = false;
        } else {
          this.flag = true;
        }
      });
      return p;
    },
    closedia() {
      this.$emit('closedia', false);
      this.$router.go(-1);
    },
    // 重新提交
    submitcar2() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.carData.amount, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认重新提交车辆维修审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                '9000B8B8D347526960C5BAB1FB6894EF17427C100F77D58DB382894F762E66D209652941AB86E5AD',
                atype: '0104',
                flowid: this.aid,
                approvalid: this.carData.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.carData;
                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                '27C5BBCC57742906E0FD67DAD8D2622896D1E3093AF33DA105E954AF4F736C20BC8C24080344A196';
                req2.bigamount = this.convertCurrency(this.carData.amount);
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '重新提交成功！',
                  });
                  this.fun();
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    submitcar() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.checksplnum(this.carData.amount, this.aid).then(() => {
          if (this.flag == false) {
            this.$message({
              type: 'error',
              message: ' 审批流不符合条件，请重新选择',
            });
            return;
          }
          this.$confirm('确认提交车辆维修审批?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              const req = {
                handle:
                '9000B8B8D347526960C5BAB1FB6894EF8B7B312373E9BEF47A8517CE4098C19A',
                atype: '0104',
                flowid: this.aid,
              };
              this.spost(this.ts, '/mainservlet', req).then((ts) => {
                const req2 = this.carData;
                req2.aid = ts.d.data.operation3[0].hash.approvalid;
                req2.handle =
                '27C5BBCC57742906E0FD67DAD8D2622896D1E3093AF33DA1CEF18ACE4D4B4EB6BFF0B9210751F692';
                req2.bigamount = this.convertCurrency(this.carData.amount);
                req2.fileurl = this.posturl(this.filearr1);
                req2.filename = this.postname(this.filearr1);
                this.spost(this.ts, '/mainservlet', req2).then(() => {
                  this.$message({
                    type: 'success',
                    message: '发起车辆维修审批成功！',
                  });
                  this.$emit('closedia', false);
                });
              });
            })
            .catch(() => {
              this.$message({
                type: 'info',
                message: '已取消',
              });
            });
        });
      }
    },
    // 公共方法图片数组转路径数组
    posturl(arr) {
      const a = [];
      for (let i = 0; i < arr.length; i += 1) {
        a.push(arr[i].url);
      }
      return a;
    },
    // 公共方法图片数组转名称数组
    postname(arr) {
      const b = [];
      for (let i = 0; i < arr.length; i += 1) {
        b.push(arr[i].name);
      }
      return b;
    },
    // 获取部门
    getselectorgid(val) {
      // console.log(val, '0909');
      this.carData.branchid = val;
      console.log(this.carData, '1');
    },
    // 获取车辆编号
    getpersonid2(val) {
      console.log(val, '6767');
      this.carData.carid = val.id2;
      console.log(this.carData, '2');
    },
    // 公共方法获取代码项
    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    getsplid(val) {
      this.aid = val;
    },
    // 金额转大写
    convertCurrency(money) {
      // 汉字的数字
      const cnNums = new Array(
        '零',
        '壹',
        '贰',
        '叁',
        '肆',
        '伍',
        '陆',
        '柒',
        '捌',
        '玖',
      );
      // 基本单位
      const cnIntRadice = new Array('', '拾', '佰', '仟');
      // 对应整数部分扩展单位
      const cnIntUnits = new Array('', '万', '亿', '兆');
      // 对应小数部分单位
      const cnDecUnits = new Array('角', '分', '毫', '厘');
      // 整数金额时后面跟的字符
      const cnInteger = '整';
      // 整型完以后的单位
      const cnIntLast = '元';
      // 最大处理的数字
      const maxNum = 999999999999999.9999;
      // 金额整数部分
      let integerNum;
      // 金额小数部分
      let decimalNum;
      // 输出的中文金额字符串
      let chineseStr = '';
      // 分离金额后用的数组，预定义
      let parts;
      if (money == '') {
        return '';
      }
      money = parseFloat(money);
      if (money >= maxNum) {
        // 超出最大处理数字
        return '';
      }
      if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
      }
      // 转换为字符串
      money = money.toString();
      if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = '';
      } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
      }
      // 获取整型部分转换
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        const IntLen = integerNum.length;
        for (var i = 0; i < IntLen; i++) {
          var n = integerNum.substr(i, 1);
          const p = IntLen - i - 1;
          const q = p / 4;
          const m = p % 4;
          if (n == '0') {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0];
            }
            // 归零
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m == 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        chineseStr += cnIntLast;
      }
      // 小数部分
      if (decimalNum != '') {
        const decLen = decimalNum.length;
        for (var i = 0; i < decLen; i++) {
          var n = decimalNum.substr(i, 1);
          if (n != '0') {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i];
          }
        }
      }
      if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum == '') {
        chineseStr += cnInteger;
      }
      return chineseStr;
    },
  },
};
</script>
